package com.gopay.bis.service.bank.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.gopay.bis.dao.tpas.TpasOrderDAO;
import com.gopay.bis.manager.bank.tpas.TpasOrderManager;
import com.gopay.bis.service.bank.TpasOrderService;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.bank.tpas.TpasMessage;
import com.gopay.common.domain.bank.tpas.TpasOrder;
import com.gopay.common.domain.bank.tpas.mwa.FlowMessage;
import com.gopay.common.domain.bank.tpas.mwa.MobileMessage;
import com.gopay.common.domain.bank.tpas.mwa.OilCardMessage;
import com.gopay.common.util.DateUtils;

public class TpasOrderServiceImpl implements TpasOrderService {
    @Resource(name = "tpasOrderManager")
    private TpasOrderManager tpasOrderManager;
    
    @Resource(name = "tpasOrderDAO")
    private TpasOrderDAO tpasOrderDAO;
    
    @Override
    public List<TpasOrder> getTpasOrdersByGopayOrderId(String orderId) {
        List<TpasOrder> orders = tpasOrderManager.getTpasOrdersByGopayOrderId(orderId);
        return orders;
    }

    @Override
    public TpasOrder addOrder(TpasMessage msg) {
        Date curDate = DateUtils.getSystemDate();
        TpasOrder order = new TpasOrder();
        order.setOrderId(msg.getOrderId());
        order.setOutOrderId(msg.getBankOrderId());
        order.setProdCode(msg.getTransType());
        order.setProdAmount(msg.getAmount()+"");
        setPervalue(msg, order);
        order.setIntrfaceChannelCode(msg.getOutStlmCd());    
        order.setSendCpTm(curDate);
        order.setOrderStatus("00");
        order.setCpRespCode("");
//        order.setTranFinishTime(tranFinishTime);
        order.setTotalCheckTimes(0);
//        order.setLastCheckBillTime(lastCheckBillTime);
//        order.setVersion(version);
        order.setCreatedTs(curDate);
        order.setLastUpdTs(curDate);
//        order.setLastUpdTrancode(lastUpdTrancode);
        order.setLock(0);
        order.setRemark2("");
        order.setRemark3("");
        
        order = tpasOrderManager.addBankOrder(order);
        return order;
    }
    
    @Override
    public TpasOrder getTpasOrderByCpOrderId(String outOrderId) {
        return tpasOrderManager.getTpasOrderByCpOrderId(outOrderId);
    }
    @Override
	public int saveCpDebitAmount(String orderId, String amount) {
    	return tpasOrderDAO.saveCpDebitAmount(orderId, amount);
	}

    @Override
    public int updateBackSuccess(String orderId) {
        return tpasOrderManager.updateBackSuccess(orderId);
    }

    @Override
    public int updateBackFail(String orderId) {
        return tpasOrderManager.updateBackFail(orderId);
    }

	@Override
	public int updateInAcctFail(String outOrderId, String status) {
		return tpasOrderManager.updateInAcctFail(outOrderId, status); 
	}
	
	private void setPervalue(TpasMessage msg, TpasOrder order) {
        if(IntTxnCd._01611.value.equals(order.getProdCode())){
            MobileMessage mobi = (MobileMessage)msg;
            order.setProdPervalue(mobi.getCardNum());
        }else if(IntTxnCd._01612.value.equals(order.getProdCode())){
            FlowMessage flow = (FlowMessage)msg;
            order.setProdPervalue(flow.getFlowValue());
        }else if(IntTxnCd._01613.value.equals(order.getProdCode())){
            OilCardMessage oil = (OilCardMessage)msg;
            order.setProdPervalue(oil.getCardId());
        }
    }

}
